package com.springboot.wisdom.common.conf;

import com.springboot.wisdom.common.common.SwaggerProperties;
import io.swagger.annotations.ApiOperation;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.*;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spi.service.contexts.SecurityContext;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

import java.util.ArrayList;
import java.util.List;

/**
 * Swagger基础配置。
 * <p>
 * 使用 Knife4j UI。
 * <p>
 * 每个Web模块都需要重写，否则无 swagger 。
 * 访问1：http://ip:port/doc.html
 */
@Configuration
@EnableSwagger2
public abstract class BaseSwaggerConfig {

    @Bean(value = "dockerBean")
    public Docket dockerBean() {
        SwaggerProperties swaggerProperties = swaggerProperties();
        //指定使用Swagger2规范
        Docket docket = new Docket(DocumentationType.SWAGGER_2)
//                .apiInfo(new ApiInfoBuilder()
//                //描述字段支持Markdown语法
//                .description("# Knife4j RESTful APIs")
//                .termsOfServiceUrl("https://doc.xiaominfo.com/")
//                .contact(new Contact("","",""))
//                .version("1.0")
//                .build())
                // 将api的元信息设置为包含在json ResourceListing响应中。
                .apiInfo(new ApiInfoBuilder()
                        .title(swaggerProperties.getTitle())
                        .description(swaggerProperties.getDescription())
                        .contact(new Contact(swaggerProperties.getContactName(), swaggerProperties.getContactUrl(), swaggerProperties.getContactEmail()))
                        .version(swaggerProperties.getVersion())
                        .build())


                //分组名称
//                .groupName("智能社区")
                .groupName(swaggerProperties.getTitle())
                .select()
                //这里指定Controller扫描包路径
//                .apis(RequestHandlerSelectors.basePackage("com.springboot.mail"))
                .apis(RequestHandlerSelectors.withMethodAnnotation(ApiOperation.class))
//                .apis(RequestHandlerSelectors.basePackage(swaggerProperties.getApiBasePackage()))
                .paths(PathSelectors.any())
                .build();

        // 授权信息设置，必要的header token等认证信息
        docket.securitySchemes(securitySchemes())
                // 授权信息全局应用
                .securityContexts(securityContexts());

        return docket;
    }

    /**
     * 授权信息设置，必要的header token等认证信息
     *
     * @return
     */
    private List<SecurityScheme> securitySchemes() {
        //设置请求头信息
        List<SecurityScheme> result = new ArrayList<>();
        SecurityScheme securityScheme = new ApiKey("token", "token", "header");
        result.add(securityScheme);
        return result;
    }

    /**
     * 授权信息全局应用
     *
     * @return
     */
    private List<SecurityContext> securityContexts() {
        //设置需要登录认证的路径
        List<SecurityContext> result = new ArrayList<>();
        result.add(getContextByPath("/*/.*"));
        return result;
    }

    private SecurityContext getContextByPath(String pathRegex) {
        return SecurityContext.builder()
                .securityReferences(defaultAuth())
                .forPaths(PathSelectors.regex(pathRegex))
                .build();
    }

    private List<SecurityReference> defaultAuth() {
        List<SecurityReference> result = new ArrayList<>();
        AuthorizationScope authorizationScope = new AuthorizationScope("global", "accessEverything");
        AuthorizationScope[] authorizationScopes = new AuthorizationScope[1];
        authorizationScopes[0] = authorizationScope;
        result.add(new SecurityReference("token", authorizationScopes));
        return result;
    }

    /**
     * 自定义Swagger配置
     */
    public abstract SwaggerProperties swaggerProperties();
}